package com.clannad.eduservice.controller.Front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clannad.commonutils.R;
import com.clannad.eduservice.entity.EduCourse;
import com.clannad.eduservice.entity.EduTeacher;
import com.clannad.eduservice.service.EduCourseService;
import com.clannad.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/eduservice/teacherfront")
@CrossOrigin
public class TeacherFrontController {

    @Autowired
    private EduTeacherService teacherService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private EduCourseService courseService;


    @PostMapping("getTeacherFrontList/{page}/{limit}")
    public R getTeacherFrontList(@PathVariable int page,@PathVariable int limit){
        Map<Object, Object> map=null;
        String key="teacherFront"+page+"ON"+limit;
        if(redisTemplate.opsForHash().entries(key)!=null&&redisTemplate.opsForHash().entries(key).size()>0){
            map=redisTemplate.opsForHash().entries(key);
        }else{
            Page<EduTeacher> page1=new Page(page,limit);
            map=teacherService.getTeacherFrontList(page1);
            redisTemplate.opsForHash().putAll(key,map);
        }
        return R.ok().data("map",map);
    }

    @GetMapping("getTeacherFrontInfo/{teacherId}")
    public R getTeacherFrontInfo(@PathVariable String teacherId){
        EduTeacher teacher = teacherService.getById(teacherId);
        LambdaQueryWrapper<EduCourse> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EduCourse::getTeacherId,teacherId);
        List<EduCourse> list = courseService.list(lambdaQueryWrapper);
        return R.ok().data("teacher",teacher).data("courseList",list);
    }

}
